import {
	formatDate
} from '@/utils/moment/index.js';
import {
	AddNewData
} from '@/common/http.request.js';
export const orderjs = {
	data() {
		return {
			currentOrderItem: {},
			show: false,
			refundForm: {
				money: "",
				content: ''
			},
		}
	},
	computed: {
		statusFilter(item, type) {
			return (item, type) => {
				let state = item.pay_status;
				let val = '';
				switch (state) {
					case -1:
					case '-1':
						val = '已关闭';
						break;
					case 0:
						val = '未支付';
						break;
					case 1:
						if (type == 4) {
							val = '待出游'
						} else if (type == 3) {
							val = '待入住'
						} else if (type == 5) {
							val = '已购买'
						} else if (type == 9) {
							val = item.is_group == 1 ? '拼团成功，待发货' : '拼团中'
						} else if (type == 2) {
							val = '待使用'
						} else {
							val = '待发货'
						}
						break;
					case 2:
						val = '退款审核中';
						break;
					case 3:
						val = '已退款';
						break;
					case 4:
						val = '已发货';
						break;
					case 5:
						val = '已收货';
						break;
					case 6:
						val = '待确认'; // 酒店,目前不支持：酒店下单后先变成6，等酒店确认后再变成待支付，然后才允许 用户支付
						break;
					case 7:
						val = '已拒单'; // 酒店
						break;
					case 8:
						val = '已入住'; // 酒店
						break;
					case 9:
						val = '已确认';
						break;
					case 10:
						val = '已完成';
						break;
					case 88:
						val = '待收货';
						if (item.ticket_status == 2) {} else {
							val = '核销完成';
						}
						if (item.pay_status == 10) val = '已完成';
						break;
				}
				return val;
			}
		},
		checkBtn(item, sign) {
			return (item, sign) => {
				let val = false;
				let state = item.pay_status;
				switch (sign) {
					case 2: // 发货
						val = (state == 1 || state == 4) ? true : false
						break;
				}
				return val;
			}
		}
	},
	filters: {
		formTime(val) {
			return formatDate(val, 'YYYY-MM-DD HH:mm:ss');
		}
	},
	methods: {
		// 退款
		openRefund(item) {
			this.currentOrderItem = {
				...item
			}
			this.refundForm = {
				money: item.total_amount,
				content: ''
			}
			this.show = true
		},
		// 确定退款
		confirmRefund() {
			if (this.refundForm.money > this.currentOrderItem.total_amount) {
				this.$toast({
					title: '金额不能大于订单金额'
				})
				return
			}
			this.$loading()
			this.$adminPost('adminApplyOrderRefund', {
				order_id: this.currentOrderItem.pk_id,
				status: 4,
				...this.refundForm
			}).then(res => {
				this.initPage()
				this.$hide()
			}).catch(() => {
				this.$hide()
			})
		},
		// 跳转订单发货
		goDeliver(item) {


			let detailed = []
			if (item.detailed[0]) {
				detailed = item.detailed.reduce(
					(pre, el) => {
						if (el.refund_status == '0' || el.refund_status == 3) {
							pre.push({
								label: el.product_name,	
								value: el.pk_id + '',
							})
						}
						return pre
					},
					[{
						label: '合并发货（未发货的商品）',
						value: '0',
					}, ]
				)
			}
			let str =
				`/package/system/order/deliver?order_no=${item.order_no}&detailed=${encodeURIComponent(JSON.stringify(detailed))}`
			if (item.pay_status == 4) {
				str += `&order_id=${item.pk_id}`
			}
			this.$linkTo(str)
		}
	}
}
